Skip to content

fix(#946): builtinTools "none" guard passes only mcp__ tools; boot test catches a dropped threadRoles lookup - #1032

Merged
scuttletoby merged 3 commits into
mainfrom
fix/946-builtin-tools-guard
Nov 22, 2026
Merged

scuttletoby merged 3 commits into
mainfrom
fix/946-builtin-tools-guard

Conversation

@scuttletoby

@scuttletoby scuttletoby commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Both follow-ups from the round-2 reviews of #927 and #928.

Item 1: builtinTools "none" guard (#927)

apps/server/src/fork/claudeMcpProfile.ts: the guard no longer returns allow when hook_event_name is not "PreToolUse". It does not read the event name at all now. A call passes only when tool_name is a string that starts with mcp__. Anything else is refused. The existing fail-closed paths are unchanged: a tool name that is not a string, or an error while checking, is refused.

Test (claudeMcpProfile.adapter.test.ts, new case): the event name is missing, undefined, "", "PostToolUse", "pretooluse" or a number. For each, Bash is refused and mcp__t3-code__t3_thread_list passes, and each call is made twice. Before the fix the test was red on the first case (Bash, event missing, try 1: expected undefined to equal 'deny'). After the fix all 13 tests in the file pass.

Item 2: end-to-end check for ThreadRoleTargets.layer (#928)

The issue left this for the later Settings PR. It was small, so it is done here.

  • apps/server/src/fork/threadRoleTargetsLayer.ts: building the layer now records a ThreadRoleTargets.make span.
  • apps/server/src/holdWork.boot.test.ts: ThreadRoleTargets.make is added to FORK_LAYER_SPANS. These are the spans the test expects from a real t3 serve boot (server.ts with everything wired).

Red, green and mutation runs:

  • Span listed in the test but not yet in the layer: red (expected [ 'ThreadRoleTargets.make' ] to deeply equal []).
  • Span added to the layer: green (2/2).
  • Mutation, ThreadRoleTargets.layer, removed from layerRuntimeServices in forkLayers.ts: red with the same message. The line was put back afterwards.

What this covers: the layer is built in the real server. Where it sits in server.ts is not new. It is in the same Layer.mergeAll as SlotGate, inside layerRuntimeCoreDependenciesBase, which the #928 review traced to the settings save.

State question

Can a hook call that is repeated, that has an old or empty event name, or that comes before the profile is read ever let a built-in tool through when the profile says none? No.

  • The guard keeps no state, so a repeated call gets the same answer (the test makes each call twice).
  • The only thing it reads is tool_name. A missing, empty, older or unknown hook_event_name cannot lead to an allow.
  • The guard is added only when the plan for a profile with builtinTools: "none" is built. That same plan also starts Claude with tools: [] and every built-in tool in disallowedTools. A process opened before the profile was read is the old process, which still has its tools. A thread that changes to none gets a new process for its next turn, because the plan key holds builtin none (as fork: MCP profile builtinTools "none" (assistant PR 1) #927 described), so no turn under none runs without the guard.
  • Item 2 stores nothing new. It only adds a span when the layer is built.

Round 1

Both lenses said safe to merge and named the same minor.

Finding Fix Test
claudeMcpProfile.ts:278: the server-list guard (claudeMcpProfileGuard) still returned allow when hook_event_name was not "PreToolUse". With servers: ["gh"] and a missing or PostToolUse event name, mcp__x__Bash (server x, not listed) passed. The line is removed, as it was for the builtinTools guard. The guard is registered only under PreToolUse (claudeMcpServersPlan; claudeMcpProfilePlan and withAgentModelPolicy keep it there), so the input is read as a PreToolUseHookInput whatever its event name. The event-name loop is now a shared helper used by both guards. New case with a servers: ["gh"] profile: for a missing, undefined, empty, "PostToolUse", "pretooluse" or numeric event name, each call made twice, mcp__x__Bash is refused and mcp__gh__list_issues passes. Red on 6adb49d (mcp__x__Bash, event missing, try 1: expected a deny), green after the fix: 14/14.

The state lens asked to leave this out: #946 names only the built-in guard, the server guard's main barriers (strict MCP config or the allowlist, and the post-start server check) are unchanged, and a second guard's answer changing needs its own tests and review. It was fixed here anyway: it is the same one-line change as item 1, it has its own test in the same pattern, and it makes this backup guard refuse by default instead of allowing on an event name it should not trust.

Not changed: a hook input with no string tool_name (and no mcp_server) still makes this guard throw, which Claude Code treats as a non-blocking error. Before this round such an input with an odd event name was allowed outright, so this is no looser than before.

Checks

  • claudeMcpProfile.adapter.test.ts: 14/14 (13/13 before round 1)
  • holdWork.boot.test.ts: 2/2
  • threadRoleTargetsLayer.test.ts: 4/4
  • vp run check:changed: format, lint, knip, graphql, typecheck and tests (5 files, 79 tests after round 1) all pass.
  • fork-seam-check.cjs origin/main HEAD: exit 0. No upstream file is changed, so there are no new seam lines.

General vs project

  • General: the guard now decides from the tool name alone and refuses anything it cannot confirm is MCP. It no longer trusts what the SDK says the event is.
  • Project: FORK_LAYER_SPANS in the boot test is this fork's way of showing that the real server wires a fork layer. ThreadRoleTargets is now one of those layers.

Fixes #946

🤖 Generated with Claude Code

…st catches a dropped threadRoles lookup

The guard no longer allows a call because hook_event_name is not
"PreToolUse": whatever the event name says (missing, empty, another
event), only an mcp__ tool passes.

ThreadRoleTargets.layer makes a "ThreadRoleTargets.make" span, and the
real-server boot test lists it with the fork's other layer spans, so a
server that loses it from layerRuntimeServices fails that test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@scuttletoby

Copy link
Copy Markdown
Owner Author

lens: code · reviewer: lens agent (Claude Code) · read at: 6adb49d
verdict: safe to merge
covered: #946 (body, no comments), the PR body, the full diff (claudeMcpProfile.ts, claudeMcpProfile.adapter.test.ts, threadRoleTargetsLayer.ts, holdWork.boot.test.ts), and around it: how ClaudeAdapterV2 merges the profile options and hooks (ClaudeAdapterV2.ts:2414-2487), withAgentModelPolicy (claudeMcpProfile.ts:711), the sibling server-list guard (claudeMcpProfile.ts:271), forkLayers.ts layerRuntimeServices, threadRoleTargets.ts:87 (Effect.serviceOption, so a dropped layer is no type error).
checks: claudeMcpProfile.adapter.test.ts 13/13 and holdWork.boot.test.ts 2/2 on the head. Red without each fix: guard file from the PR's parent, the new test fails Bash, event missing, try 1: expected undefined to equal 'deny'; span removed from threadRoleTargetsLayer.ts, boot test fails expected [ 'ThreadRoleTargets.make' ] to deeply equal []; ThreadRoleTargets.layer, removed from layerRuntimeServices, same red. All files put back.
checks: a throwaway probe (not committed) ran every PreToolUse hook of the real plan for servers: "all" and servers: ["gh"], both with builtinTools "none", under event names PreToolUse, missing and PostToolUse. Refused every time: Bash, bash, BASH, " Bash", Bash plus a zero-width space, Task, Agent, MultiEdit, NotebookEdit, WebFetch, Read, Edit, Write, KillShell, BashOutput, MCP__x__y, Mcp__x, mcp_x, mcp, a Cyrillic-с and a full-width-m look-alike of mcp__x, " mcp__x", zero-width-prefixed mcp__x. Passed: only names that start with the exact bytes mcp__ (including bare mcp__ and mcp__Bash). None of the SDK's 54 BUILTIN_TOOL_NAMES (0.3.321) starts with mcp, and Claude runs a tool by its exact name from its loaded list, which under "none" holds no built-in (tools: [], spread after the adapter's own set at ClaudeAdapterV2.ts:2458), so an mcp__ name can only reach an MCP tool. Old aliases (Task, MultiEdit, KillShell) are not in BUILTIN_TOOL_NAMES, so disallowedTools misses them, but the guard refuses them.
checks: tool paths. The guard is a PreToolUse matcher with no matcher, so it runs for every tool call, MCP ones and sub-agent ones included; Agent itself is refused, so no sub-agent starts. A PreToolUse deny comes before canUseTool and permission mode, and in Claude Code a deny from any hook wins over another hook's allow. Not run against a real Claude binary: the test calls the hooks the way the CLI does. Shell that Claude runs outside a tool call (a user-typed slash command's ! lines, settings-file hooks) never reaches PreToolUse. That was true before this PR too, and it is not a call the model makes.
checks: the boot test's limits are stated truthfully in the body: the span proves the real server builds the layer, not where it sits. Its placement next to SlotGate is unchanged and was traced in #928.
state: runs again: the guard keeps no state, so a second call gets the same answer (the test makes each call twice). An old, missing, empty, other or non-string event name: only tool_name is read, so it cannot lead to an allow. A process opened before the profile said "none" still has its tools, but the plan key holds builtin none, so the next turn opens a new process with the guard (existing test at claudeMcpProfile.adapter.test.ts:401). The span is only a trace on layer build and stores nothing.
blocker: none
major: none
minor: claudeMcpProfile.ts:278, the sibling server-list guard (claudeMcpProfileGuard) still has if (input.hook_event_name !== "PreToolUse") return {};. The probe: with servers: ["gh"] and a missing or PostToolUse event name, mcp__x__Bash (server x, not listed) passes. Under "none" no built-in tool gets through, because the builtinTools guard still refuses it, so this is not a hole in this PR's guard. It is the same defence-in-depth gap as #946 item 1, only for servers. Small and in scope: drop that line, reuse the new test's event-name loop with a server-list profile, and fix it in this PR.

🤖 Generated with Claude Code

@scuttletoby

Copy link
Copy Markdown
Owner Author

lens: state · reviewer: lens agent · read at: 6adb49d
verdict: safe to merge
covered: #946 (body, no comments), the PR body, the full diff (4 files); read around it in claudeMcpProfile.ts (plan, plan key, opened-plan keys, both PreToolUse guards), mcpProfileResolve.ts (how an entry and builtinTools are read), threadRoleTargets.ts, threadRoleTargetsLayer.ts, forkLayers.ts, the save check in serverSettings.ts, server.ts layerRuntimeCoreDependenciesBase
checks: claudeMcpProfile.adapter.test.ts 13/13 and threadRoleTargetsLayer.test.ts 4/4 pass here. Red check of item 1: put if (input.hook_event_name !== "PreToolUse") return {}; back in the guard: the new test fails (Bash, event missing, try 1: expected undefined to equal 'deny'); put back: green. holdWork.boot.test.ts could not run on this machine (it boots the real server on a local socket, blocked here; timed out at 400s), so its red check (drop ThreadRoleTargets.layer from layerRuntimeServices) is the PR's own run plus CI; read statically, the span is made only inside that layer's build, so a dropped layer leaves it out of the boot trace and missingForkLayers lists it
state: the guard keeps no state and reads only tool_name: a repeated call, or one whose event name is missing, undefined, empty, another event, wrong case or not a string, gets the same answer (deny unless the name is a string starting mcp__; a non-object input throws and the catch denies). It is registered only under PreToolUse, so dropping the event check changes no other event. Profile not read yet, unreadable or missing: the turn is refused before Claude opens (unchanged, tested). An old saved profile without builtinTools: read as absent, which is the documented "all built-in tools" and adds no guard, as before; an unknown value refuses the entry, so nothing falls back to allow-all because of the key. Profile changed while a session runs: builtin none is part of the plan key, so the next turn opens a new process with tools: [], the disallow list and the guard; the running turn keeps its old process until it ends (unchanged from #927). A session from before the change: after the server restarts, the opened-plan keys are empty and the process is opened again from the current plan, with the new guard. Item 2 stores nothing: the span is recorded once when the layer is built; find reads getThreadShell fresh on each save, so a save while a thread runs sees that thread. The PR's stated limit is true: the boot test proves the real server builds the layer, not that the settings write fiber's context holds it (Effect.serviceOption in threadRoleTargets.ts:87); that wiring is the unchanged one #928 traced
blocker: none
major: none
minor: claudeMcpProfile.ts:278, the servers-list guard (claudeMcpProfileGuard) still returns allow when hook_event_name is not "PreToolUse", the same pattern this PR removes from the built-in guard. Drop it here: #946 names only the built-in guard, that guard's main barriers (strict MCP config or the allowlist, and the post-start server check that closes the session) are unchanged, and changing a second guard's answer needs its own tests and review

🤖 Generated with Claude Code

scuttletoby and others added 2 commits November 22, 2026 22:25
…ts event name

The server-list guard returned allow when hook_event_name was not
"PreToolUse", so with servers ["gh"] and a missing or other event name a
call like mcp__x__Bash (server x, not listed) passed. The guard is
registered only under PreToolUse, so the check is dropped, as it was for
the builtinTools "none" guard.

The event-name test loop is shared by both guards: for a missing,
undefined, empty, "PostToolUse", "pretooluse" or numeric event name, each
call made twice, a left-out server's tool is refused and a listed
server's tool passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@scuttletoby
scuttletoby merged commit 92061ae into main Nov 22, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

builtinTools guard and threadRoles wiring: follow-ups from #927/#928 reviews

1 participant